/**
 * @description 
 * 
 * Copyright (c) 2015-2017 zhangxingong,Inc.All Rights Reserved.
 */
package com.vcredit.creditcat.manager.product;

import java.util.List;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.vcredit.creditcat.api.ICustProductApplyManager;
import com.vcredit.creditcat.bean.SpringContextHolder;
import com.vcredit.creditcat.enumtype.MerchantProductEnum;
import com.vcredit.creditcat.exception.ProductCanNotApplyException;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.model.CustProductApply;
import com.vcredit.creditcat.model.MerchantProduct;
import com.vcredit.creditcat.service.CustProductApplyService;
import com.vcredit.creditcat.service.CustService;
import com.vcredit.creditcat.service.MerchantProductService;
import com.vcredit.creditcat.service.product.IProductApplyService;
import com.vcredit.creditcat.service.product.util.EnumExtUtil;
import com.vcredit.framework.annotation.Log;

/**
 * @author 张新功
 */
@Service(protocol = {"protocol1"}, version = "1.0")
public class CustProductApplyManager implements ICustProductApplyManager {
  @Autowired
  private CustService custService;
  @Autowired
  private CustProductApplyService custProductApplyService;
  @Autowired
  private MerchantProductService merchantProductService;

  @Log
  @Override
  public void queryLimitThatWaitLongTimeAndNoResponse(CustProductApply obj)
      throws ProductCanNotApplyException {

    if (ObjectUtils.equals(null, obj) || ObjectUtils.equals(null, obj.getCustId())) {
      return;
    }

    Cust custObject = custService.findById(obj.getCustId());

    if (ObjectUtils.equals(null, custObject) || StringUtils.isEmpty(custObject.getCustIdno())) {
      return;
    }

    try {

      MerchantProduct merchant = merchantProductService.findById(obj.getProductId());
      MerchantProductEnum productEnumByName =
          EnumExtUtil.getEnumOnValue(MerchantProductEnum.class, merchant.getProductName(), "name");
      IProductApplyService applyService =
          (IProductApplyService) SpringContextHolder.getBean(productEnumByName.name());
      applyService.queryLimit(obj);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  @Log
  @Override
  public List<CustProductApply> findRequireQuryLimitList() {
    return custProductApplyService.findRequireQuryLimitList();
  }
  
  @Log
  @Override
  public CustProductApply findNewApplyProduct(Long productId, Long custId) {
    return custProductApplyService.findNewApplyProduct(productId,custId);
  }
}
